/** Tank object class that has
 * fields for all of the attributes
 * that the tank will need.
 * Also has a reverse field
 * for reversing the sprite.
 * 
 * 
 * NOTE FOR USE:
 * If changing an attribute
 * that is used in draw()
 * please note that draw()
 * is a function in object
 * and not TankObject. This
 * means you have to change
 * the attribute in the super
 * class object before it has
 * an effect in draw().
 * 
 * @author Andrew
 * @creator Andrew
 * LAST UPDATED - Andrew 2/4/12
 */


public class TankObject extends Object {
    // Health of tank
    private int health;
    
    // Is it alive
    private boolean alive;
    
    // Sprite Reference for object
    public String sprite = "cata.png";
    
    // Internal timer for animations
    private Timer frameTimer = new Timer();
    
    // Is an Animation playing?
    private boolean animation;
    
    // Do we want an Animation Playing?
    private boolean ani;
    
    // Type of Ammo
    // 0 - standard
    // 1 - land ball
    // 2 - explosive
    // 3 - cat
    // 4 - multi ball
    private int ammo;
    
    // the amount of ammo left for ammo 1-5
    private int ammo1;
    private int ammo2;
    private int ammo3;
    private int ammo4;
    private int ammo5;
    
    // AmmoBar
    private AmmoBar ab;
    
    // Fire Frames per milisecond
    private int Firefps = 800/10;
    
    // Sprite List for all sprites needed during animation
    public Sprite[] spritelist = { new Sprite("cata1.png"),
                                   new Sprite("cata2.png"),
                                   new Sprite("cata3.png"),
                                   new Sprite("cata4.png"),
                                   new Sprite("cata5.png"),
                                   new Sprite("cata6.png"),
                                   new Sprite("cata7.png"),
                                   new Sprite("cata8.png"),
                                   new Sprite("cata9.png"),
                                   new Sprite("cata10.png")};
    
    
    // Is object reversed?
    private boolean reversed = false;
    
    // Sound effects
    private Sound fireSound;
    // :)
    private Sound meow;
    
    // HealthBar
    private HealthBar hb = new HealthBar(x,y);
    
    // LaserSight
    public LaserSite ls = new LaserSite(x+50,y+48,-45.0d);
    
    // create a new Object with a sprite
  public TankObject(double x, double y,String sprite) {
      super((new Sprite(sprite)),x,y);
      this.animation = false;
      this.ani = false;
      this.health = 5;
      this.alive = true;
      this.ammo = 0;
      fireSound = new Sound("cata-throw.ogg");
      meow = new Sound("meow.ogg");
      this.ab = new AmmoBar(55,50,this);
      
      // starting ammos
      this.ammo1 = 25;
      this.ammo2 = 7;
      this.ammo3 = 10;
      this.ammo4 = 2;
      this.ammo5 = 5;
  }
  
  // move the health bar
  public void adjustAmmoBar(int xii,int yii) {
	  this.ab = new AmmoBar(xii,yii,this);
  }
  
  // change ammo number ammoNumber to have amount n
  public void setAmmo(int ammoNumber, int n) {
	  if(ammoNumber == 0) this.ammo1 = n;
	  else if(ammoNumber == 1) this.ammo2 = n;
	  else if(ammoNumber == 2) this.ammo3 = n;
	  else if(ammoNumber == 3) this.ammo4 = n;
	  else this.ammo5 = n;
  }
  
  // How much ammo is left in ammoNumber?
  public int getAmmunition(int ammoNumber) {
	  if(ammoNumber == 0) return this.ammo1;
	  else if(ammoNumber == 1) return this.ammo2;
	  else if(ammoNumber == 2) return this.ammo3;
	  else if(ammoNumber == 3) return this.ammo4;
	  else return this.ammo5;
  }
  
  // Change the health of this tank
  public void changeHealth(int n) {
      this.health -= n;
      if(health <= 0) {
          this.alive = false;
      }
  }
  
  // Move the health bar of this tank
  public void adjustHealthBar(int xii, int yii) {
      this.hb = new HealthBar(xii, yii);
      this.hb.update();
  }
  
  // What ammo is this tank using?
  public int getAmmo() {
	  return this.ammo;
  }
  
  // Change the ammo it is using and update the ammo bar accordingly
  public void setAmmo(int n) {
	  this.ammo = n;
	  this.ab.update(this);
  }
  
  // draw the tank and its health bar
  // lasersight and ammo bar
  // and reversing if needed
  
  public void draw() {
	  if(reversed) {
		  super.drawReversed();
	      ls.drawReveresed();
	      ab.update(this);
	      ab.draw();
	      hb.draw(this.health);
	  }
	  else {
		  super.draw();
	      ls.draw();
	      ab.update(this);
	      ab.draw();
	      hb.draw(this.health);
	  }
      
  }
  
  // If the target is Alive return true
  public boolean isAlive() {
      return this.alive;
  }
  
  // Return the health of this tank
  public int getHealth() {
      return this.health;
  }
  
  // set the health of this tank to n
  public void setHealth(int n) {
	  this.health = n;
  }
  
  // reverses object by loading new sprite
  public void reverse() {
      reversed = !reversed;
  }
  
  // Function which runs a firing animation
  // returns true when it is ok to fire the ball.
  public boolean fire() {
	  // if just starting to fire
      if(this.animation == false &&
         this.ani == false) {
    	  // play the correct sound
    	  if(this.ammo == 3) {
    		  this.meow.play();
    	  }
    	  else this.fireSound.play();
    	  // and set the timers, and booleans
          frameTimer.reset();
          this.animation = true;
          this.ani = true;
          return false;
      }
      // if the animation is done, keep going
      else if(this.animation == true &&
              this.ani == false) {
          return true;
          // but do nothing
      }
      // but if it isnt done, keep drawing, return true if you can fire the ball, false if you cant
      else {
        if(frameTimer.getDeltaTime()<=Firefps) { super.sprite = spritelist[0];  return false; }
        else if(frameTimer.getDeltaTime()>Firefps && frameTimer.getDeltaTime()<=Firefps*2) { super.sprite = spritelist[1]; return false; }
        else if(frameTimer.getDeltaTime()>Firefps*2 && frameTimer.getDeltaTime()<=Firefps*3){ super.sprite = spritelist[2]; return false; }
        else if(frameTimer.getDeltaTime()>Firefps*3 && frameTimer.getDeltaTime()<=Firefps*4){  super.sprite = spritelist[3]; return false; }
        else if(frameTimer.getDeltaTime()>Firefps*4 && frameTimer.getDeltaTime()<=Firefps*5){  super.sprite = spritelist[4]; return false; }
        else if(frameTimer.getDeltaTime()>Firefps*5 && frameTimer.getDeltaTime()<=Firefps*6){  super.sprite = spritelist[5];  return true; }
        else if(frameTimer.getDeltaTime()>Firefps*6 && frameTimer.getDeltaTime()<=Firefps*7){  super.sprite = spritelist[6];  return true; }
        else if(frameTimer.getDeltaTime()>Firefps*7 && frameTimer.getDeltaTime()<=Firefps*8){  super.sprite = spritelist[7];  return true; }
        else if(frameTimer.getDeltaTime()>Firefps*8 && frameTimer.getDeltaTime()<=Firefps*9){  super.sprite = spritelist[8];  return true; }
        else if(frameTimer.getDeltaTime()>Firefps*9 && frameTimer.getDeltaTime()<=Firefps*10){  super.sprite = spritelist[9];  return true; }
        else {
          super.sprite = spritelist[0];
          this.animation = false;
          return true;
      }
      }
  }
  
  // resets the time value, the sprite and the the animation boolean
  public void reset() {
      super.sprite = spritelist[0];
      this.ani = false;
      this.frameTimer.reset();
  }
  
}
